﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    internal class SetResolutionWizard : TerrainWizard
    {
        public int m_BaseTextureResolution = 0x400;
        public int m_ControlTextureResolution = 0x400;
        public int m_DetailResolution = 0x400;
        public int m_DetailResolutionPerPatch = 0x10;
        public int m_HeightmapResolution = 0x400;
        public float m_TerrainHeight = 600f;
        public float m_TerrainLength = 2000f;
        public float m_TerrainWidth = 2000f;

        internal void InitializeDefaults(Terrain terrain)
        {
            base.m_Terrain = terrain;
            this.m_TerrainWidth = base.terrainData.size.x;
            this.m_TerrainHeight = base.terrainData.size.y;
            this.m_TerrainLength = base.terrainData.size.z;
            this.m_HeightmapResolution = base.terrainData.heightmapResolution;
            this.m_DetailResolution = base.terrainData.detailResolution;
            this.m_DetailResolutionPerPatch = base.terrainData.detailResolutionPerPatch;
            this.m_ControlTextureResolution = base.terrainData.alphamapResolution;
            this.m_BaseTextureResolution = base.terrainData.baseMapResolution;
            this.OnWizardUpdate();
        }

        private void OnWizardCreate()
        {
            if (this.m_HeightmapResolution >= 0x1001)
            {
                base.isValid = false;
                base.errorString = "Heightmaps above 4096x4096 in resolution are not supported";
                Debug.LogError("Heightmaps above 4096x4096 in resolution are not supported");
            }
            else
            {
                ArrayList list = new ArrayList();
                list.Add(base.terrainData);
                list.AddRange(base.terrainData.alphamapTextures);
                Undo.RegisterUndo(base.terrainData, list.ToArray(typeof(UnityEngine.Object)) as UnityEngine.Object[], "Set Resolution");
                if (base.terrainData.heightmapResolution != this.m_HeightmapResolution)
                {
                    base.terrainData.heightmapResolution = this.m_HeightmapResolution;
                }
                base.terrainData.size = new Vector3(this.m_TerrainWidth, this.m_TerrainHeight, this.m_TerrainLength);
                if ((base.terrainData.detailResolution != this.m_DetailResolution) || (this.m_DetailResolutionPerPatch != base.terrainData.detailResolutionPerPatch))
                {
                    ResizeDetailResolution(base.terrainData, this.m_DetailResolution, this.m_DetailResolutionPerPatch);
                }
                if (base.terrainData.alphamapResolution != this.m_ControlTextureResolution)
                {
                    base.terrainData.alphamapResolution = this.m_ControlTextureResolution;
                }
                if (base.terrainData.baseMapResolution != this.m_BaseTextureResolution)
                {
                    base.terrainData.baseMapResolution = this.m_BaseTextureResolution;
                }
                base.FlushHeightmapModification();
            }
        }

        internal override void OnWizardUpdate()
        {
            base.helpString = "Please note that modifying the resolution will clear the heightmap, detail map or splatmap.";
            base.OnWizardUpdate();
            if (base.terrainData != null)
            {
                this.m_HeightmapResolution = base.terrainData.GetAdjustedSize(this.m_HeightmapResolution);
            }
            if (this.m_TerrainWidth <= 0f)
            {
                this.m_TerrainWidth = 1f;
            }
            if (this.m_TerrainHeight <= 0f)
            {
                this.m_TerrainHeight = 1f;
            }
            if (this.m_TerrainLength <= 0f)
            {
                this.m_TerrainLength = 1f;
            }
            this.m_ControlTextureResolution = Mathf.Clamp(Mathf.ClosestPowerOfTwo(this.m_ControlTextureResolution), 0x10, 0x800);
            this.m_BaseTextureResolution = Mathf.Clamp(Mathf.ClosestPowerOfTwo(this.m_BaseTextureResolution), 0x10, 0x800);
            this.m_DetailResolution = Mathf.Clamp(this.m_DetailResolution, 0, 0xfd0);
            this.m_DetailResolutionPerPatch = Mathf.Clamp(this.m_DetailResolutionPerPatch, 8, 0x80);
        }

        private static void ResizeDetailResolution(TerrainData terrainData, int resolution, int resolutionPerPatch)
        {
            if (resolution == terrainData.detailResolution)
            {
                List<int[,]> list = new List<int[,]>();
                for (int i = 0; i < terrainData.detailPrototypes.Length; i++)
                {
                    list.Add(terrainData.GetDetailLayer(0, 0, terrainData.detailWidth, terrainData.detailHeight, i));
                }
                terrainData.SetDetailResolution(resolution, resolutionPerPatch);
                for (int j = 0; j < list.Count; j++)
                {
                    terrainData.SetDetailLayer(0, 0, j, list[j]);
                }
            }
            else
            {
                terrainData.SetDetailResolution(resolution, resolutionPerPatch);
            }
        }
    }
}

